package com.orange.cepmobile.android.service;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.SmsMessage;
import android.util.Log;
import android.widget.Toast;

import com.orange.cepmobile.android.CEPMobileCommon;
import com.orange.cepmobile.android.activity.Home;
import com.orange.cepmobile.android.ngsi.events.Event;

public class EventReceiver extends BroadcastReceiver {
	public static final String TAG = CEPMobileCommon.EVENT_RECEIVER_TAG;
	private Service service;
	private Event event;
	private String message="event!";

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		Log.i(TAG, " EventReceiver : event !!");
		Toast.makeText(context, "reception of event ", Toast.LENGTH_LONG)
				.show();
		service = null;
		event = null;
		final String action = intent.getAction();
		if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
			onReceiveBoot(context, intent);
			return;
		}
		service = Service.getInstance();
		if (service == null || !Service.isEnabled(context)
				|| !Service.isRunning())
			return;
		event = new Event();
		if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(action))
			onReceivePhoneStateChanged(context, intent);
		else if (Intent.ACTION_NEW_OUTGOING_CALL.equals(action))
			onReceiveOutgoingPhoneCall(context, intent);
		else if (CEPMobileCommon.ACTION_SMS_RECEIVED.equals(action))
			onReceiveSmsReceived(context, intent);
		else {// Default event code
			event.eventType = action;
			final String data = intent.getDataString();
			Log.v(TAG, "broadcast : action=" + action + ", data=" + data);
			service.acceptIncomingEvent(event);
		}

	}

	private void onReceiveBoot(final Context context, final Intent intent) {
		Log.v(TAG, "boot completed");
		if (Service.isEnabled(context) && !Service.isRunning())
			Service.start(context, false);
	}

	private void onReceiveOutgoingPhoneCall(final Context context,
			final Intent intent) {
		final String phoneNumber = intent
				.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
		event.eventType = CEPMobileCommon.EVENT_TYPE_OUTGOING_PHONE_CALL;
		final StringBuilder sb = new StringBuilder();
		if (phoneNumber != null)
			sb.append("number:" + phoneNumber);
		event.params = sb.toString();
		service.acceptIncomingEvent(event);
	}

	private void onReceivePhoneStateChanged(final Context context,
			final Intent intent) {
		final String phoneState = intent
				.getStringExtra(TelephonyManager.EXTRA_STATE);
		final String phoneNumber = intent
				.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
		event.eventType = CEPMobileCommon.EVENT_TYPE_PHONE_STATE_CHANGED;
		final StringBuilder sb = new StringBuilder();
		if (TelephonyManager.EXTRA_STATE_IDLE.equals(phoneState))
			sb.append("idle");
		else if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(phoneState))
			sb.append("offhook");
		else if (TelephonyManager.EXTRA_STATE_RINGING.equals(phoneState))
			sb.append("ringing");
		else
			sb.append(phoneState);
		if (phoneNumber != null) {
			sb.append(',');
			sb.append("number:" + phoneNumber);
		}
		event.params = sb.toString();
		message=" event : reception sms !";
		service.acceptIncomingEvent(event);
	}

	private void onReceiveSmsReceived(final Context context, final Intent intent) {
		Toast.makeText(context, "reception of sms ", Toast.LENGTH_LONG)
		.show();
		event.eventType = CEPMobileCommon.EVENT_TYPE_SMS_RECEIVED;
		final Bundle bundle = intent.getExtras();
		if (bundle != null) {
			final Object[] pdus = (Object[]) bundle.get("pdus");
			final SmsMessage[] messages = new SmsMessage[pdus.length];
			final StringBuilder sb = new StringBuilder();
			for (int i = 0; i < messages.length; i++) {
				if (i > 0)
					sb.append(',');
				messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
				sb.append("from ");
				sb.append(messages[i].getOriginatingAddress());
				sb.append(":[");
				sb.append(messages[i].getMessageBody().toString());
				sb.append(']');
			}
			event.params = sb.toString();
			Log.i(TAG, "message"+event.params);
		}
		service.acceptIncomingEvent(event);
		//Home.ShowAlarm();
	}
	
}
